package com.zxy.ziems.server.storage;

import com.zxy.ziems.common.mqtt.BaseListMqttConsumer;
import com.zxy.ziems.common.mqtt.annotation.MqttTopic;
import com.zxy.ziems.common.mqtt.pojo.Pattern;
import com.zxy.ziems.common.pojo.MeasureDataBean;
import com.zxy.ziems.server.alarm.service.AlarmRecordService;
import com.zxy.ziems.server.storage.service.MeasureDataStorageService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Lazy;
import org.springframework.stereotype.Component;

import java.time.LocalDateTime;
import java.util.List;

/**
 * 设备测量数据存储监听
 *
 * @author 10348359
 * @create 2024-04-29 10:23
 */
@Slf4j
@Component
@MqttTopic(
        topic = "${zxy.topic.ammeter-data}",
        patten = Pattern.SHARE,
        group = "ammeterDataStorage",
        qos = 1
)
public class AmmeterDeviceMeasureDataStorageListener extends BaseListMqttConsumer<MeasureDataBean> {

    @Override
    protected void doProcessMessage(String topic, List<MeasureDataBean> list, LocalDateTime sendTime) {
        log.info("[MQTT]接收数据");
        storageService.storageMeasureDataList(list);
        alarmRecordService.storageAlarmByAlamRule(list);
    }

    private MeasureDataStorageService storageService;
    private AlarmRecordService alarmRecordService;

    @Lazy
    @Autowired
    public void setStorageService(MeasureDataStorageService measureDataStorageService) {
        this.storageService = measureDataStorageService;
    }

    @Lazy
    @Autowired
    public void setAlarmRecordService(AlarmRecordService alarmRecordService) {
        this.alarmRecordService = alarmRecordService;
    }
}
